package yawning.mew.bear;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.bear.BearThreatSimulation.SimulatorProperty;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;

public class MaulAction extends SimulationEventAction {
	BearThreatSimulator mState;
	double mCooldownExpireTime = 0.0;
	private boolean mExternalBleed;

	MaulAction(BearThreatSimulator state) {
		super(state.mSimEngine);
		mState = state;

		mExternalBleed = mState.mSimEngine.getConfig().getBool(SimulatorProperty.ASSUMEEXTERNALBLEED);
	}

	@Override
	public double perform() {
		int cost = mState.mModel.maulCost;

		if (!mState.hasRage(cost) && !mState.mOoCEv.isUp()) return SimulationAction.ACTION_FAIL;
		if (mCooldownExpireTime > mState.mSimEngine.getSimTime()) return SimulationAction.ACTION_FAIL;

		AttackResult result = mState.mModel.getFrontalYellowResult();

		if (result == AttackResult.MISS) {
			mNrMaulMiss++;

			if (mState.mIsLogging) mState.mLog.log("Maul: MISS\n");
		} else if (result == AttackResult.DODGE) {
			mNrMaulDodge++;

			if (mState.mIsLogging) mState.mLog.log("Maul: DODGE\n");
		} else if (result == AttackResult.PARRY) {
			mNrMaulParry++;

			if (mState.mIsLogging) mState.mLog.log("Maul: PARRY\n");
		} else {
			double damage =  mState.mModel.mLevelDep.getMaulBaseDmg() + mState.mModel.getAttackPower() * mState.mModel.mLevelDep.getMaulCoeff();

			if (result == AttackResult.CRIT) {
				mNrMaulCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrMaulHit++;
			}

			damage *= (mState.mMangleAction.isUp()) ? mState.mModel.multMangle : 1.0;

			if (mExternalBleed || mState.mThrashAction.isUp() || mState.mLacerateAction.isUp()) {
				damage *= mState.mModel.multMaulRnT;
			}

			damage *= mState.getDirectDamageMult();
			mMaulDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("Maul: %s Damage: %d\n", result, Math.round(damage));

			mState.mInfectedWoundsEv.onTrigger();

			if (mState.mModel.mToon.tier_12_2pc) mState.mTier_12_2pcEv.run(damage);

			if (result == AttackResult.CRIT) {
				mState.mSavageDefenseEv.onTrigger();
				mState.mLotPEv.onTrigger();
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOWCRIT);
			} else {
				mState.mSimEngine.triggerProc(mState.mActiveTarget, Trigger.YELLOW);
			}
		}

		mCooldownExpireTime = mState.mSimEngine.getSimTime() + mState.mModel.maulCDDuration;
		schedule(mState.mModel.maulCDDuration);

		mState.onRageLoss(cost);

		if (result == AttackResult.CRIT) mState.onPrimalFuryTrigger();

		return 0.0;
	}

	long mMaulDamage = 0;
	int mNrMaulMiss = 0;
	int mNrMaulDodge = 0;
	int mNrMaulParry = 0;
	int mNrMaulCrit = 0;
	int mNrMaulHit = 0;
}
